package com.ziyun.erp.web.modules.business.controller;

import com.cloud.bus.core.EventBusPusher;
//import com.cloud.bus.event.goods.BatchUpdateGoodsEvent;
import com.cloud.bus.event.goods.UpdateGoodsCacheEvent;
import com.utility.StringUtils;
import com.ziyun.erp.common.utils.UserUtils;
import com.ziyun.erp.modules.business.entity.GoodsPushEntity;
import com.ziyun.erp.modules.business.service.GoodsPushService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 商品搜索推荐表
 *
 * @author jiangli
 * @email 
 * @date 2018-12-18 17:16:31
 */
@RestController
@RequestMapping("goodsPush/goodspush")
public class GoodsPushController {
    @Autowired
    private GoodsPushService goodsPushService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("goodsPush:goodspush:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = goodsPushService.list(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{goodsId}")
    @RequiresPermissions("goodsPush:goodspush:info")
    public R info(@PathVariable("goodsId") Long goodsId){
        GoodsPushEntity goodsPush = goodsPushService.findOne(goodsId);

        return R.ok().put("goodsPush", goodsPush);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("goodsPush:goodspush:save")
    public R save(@RequestBody GoodsPushEntity goodsPush){
        goodsPush.setCreateUser(UserUtils.getCurentUser().getUsername());
        goodsPushService.insert(goodsPush);
        UpdateGoodsCacheEvent event = UpdateGoodsCacheEvent.builder().source(this).goodsId(goodsPush.getGoodsId()).build();
        EventBusPusher.syncPost(event);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("goodsPush:goodspush:update")
    public R update(@RequestBody GoodsPushEntity goodsPush){
        ValidatorUtils.validateEntity(goodsPush);
        goodsPush.setUpdateUser(UserUtils.getCurentUser().getUsername());
        goodsPushService.updateAllColumnById(goodsPush);//全部更新
        UpdateGoodsCacheEvent event = UpdateGoodsCacheEvent.builder().source(this).goodsId(goodsPush.getGoodsId()).build();
        EventBusPusher.syncPost(event);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("goodsPush:goodspush:delete")
    public R delete(@RequestBody Long[] goodsIds){
//        goodsPushService.deleteBatchIds(Arrays.asList(goodsIds));
//        BatchUpdateGoodsEvent event = BatchUpdateGoodsEvent.builder().source(this).goodsIds(StringUtils.join(goodsIds, ",")).build();
//        EventBusPusher.syncPost(event);
        return R.ok();
    }

    //查找所以商品
    @GetMapping("/goodsList")
    public R goodsList(){
        List<Map<String, Object>> goodsList = goodsPushService.goodsList();
        return R.ok().put("goodsList",goodsList);
    }

}
